import java.io.*;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        //System.out.println("Hello world!");

        String filePath = "newData.txt";
        List<CurveVo> curveVoList = new ArrayList<>();

        for(int i = 0; i < 47;i++){
            CurveVo curveVo = new CurveVo();
            curveVo.setSensorno(i+1);
            curveVo.setAbsolutetime(new ArrayList<>());
            curveVo.setRelativetetime(new ArrayList<>());
            curveVo.setSensorvalues(new ArrayList<Double>());
            curveVoList.add(curveVo);
        }


        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            // 逐行读取文件内容
            while ((line = reader.readLine()) != null) {
                // 打印当前行的内容
                // System.out.println(line);
                String[] parts = line.split("\t");

                String primaryKey = parts[0].substring(1, parts[0].length() - 1);
                String relativetetime = parts[1].substring(1, parts[1].length() - 1);

                // System.out.println(primaryKey); // 相对时间
                // System.out.println(relativetetime); // 相对时间

                // 设置传感器的相对时间(分钟)
                DecimalFormat dec = new DecimalFormat("0.00");
                double seconds =  Double.parseDouble(String.valueOf(relativetetime)) * 60;

                // 四舍五入,保留0位小数
                String formatSecond = new Formatter().format("%.0f", seconds).toString();
                String formatSeconds=dec.format(seconds);

                // System.out.println(seconds);
                //System.out.println("seconds "+formatSeconds);

                String[] segments = parts[2].substring(2, parts[2].length()-1).split("@");
                // System.out.println(parts[2].substring(2, parts[2].length()-1));
                for (int j = 0; j < segments.length; j++) {
                    // System.out.println(segment);
                    String[] sensor = segments[j].split(":");
                    //System.out.println(sensor[0] + ":" + sensor[1]);
                    Integer sensorNo = Integer.parseInt(sensor[0]) - 1;
                    // System.out.println(sensorNo);
                    String sensorValue = sensor[1];
//                    if(sensorNo == 1)
//                        System.out.println(parts[2]);

                    if (sensorValue.equals("9999999") || sensorValue.equals("8888888")) {

                    } else {
                        //3.1 设置传感器值
                        curveVoList.get(sensorNo).getSensorvalues().add(Double.parseDouble(StringUtil.isRealNumber(sensorValue) ? sensorValue : "0"));
                    }

                    //3.2 设置相对时间
                    curveVoList.get(sensorNo).getRelativetetime().add(formatSeconds);

                    //3.3 设置绝对时间
                    StringBuilder sb = new StringBuilder(primaryKey);

                    //DateTime dt = new DateTime(sb.insert(10, "T").substring(0, 19));
                    String dateTimeStr = sb.insert(10, "T").substring(0, 19);
                    // System.out.println(dateTimeStr);
                    LocalDateTime dt = LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
                    dt = dt.plusSeconds(Integer.parseInt(formatSecond));
                    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
                    String formattedTime = dt.format(timeFormatter);
                    curveVoList.get(sensorNo).getAbsolutetime().add(formattedTime);
                    // curveVoList.get(sensorNo).getAbsolutetime().add(dt.toString("HH:mm:ss"));
                    // 3.4 设置测试总时间
                    curveVoList.get(sensorNo).setTotaltime(formatSeconds);

                }
            }
            System.out.println(curveVoList.get(1).getSensorvalues().size());
            // System.out.println(curveVoList.get(1));
        } catch (IOException e) {
            // 捕捉并处理文件读取过程中的异常
            e.printStackTrace();
            System.err.println("An error occurred while reading the file.");
        }

    }
}